/**
给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
输入：head = [1,2,3,4,5], n = 2
输出：[1,2,3,5] 
 */
function removeNthFromEnd(head, n) {
  let dummyHead = new ListNode(0)
  dummyHead.next = head
  let slow = dummyHead
  let fast = dummyHead
  for(let i = 0; i < n; i++) {
    fast = fast.next
  }
  while(fast&&fast.next) {
    fast = fast.next
    slow = slow.next
  }
  slow.next = slow.next.next // 删除倒数第n个节点 slow.next
  return dummyHead.next
}
const head = [1,2,3,4,5]
const n = 2
console.log(removeNthFromEnd(head, n))
/**
idea: 双指针，快指针先走n步，然后快慢指针一起走，当快指针走到链表末尾时，慢指针就是倒数第n个节点
 */